home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-25 | 2.1 KB | 82 lines | [TEXT/KEEN] |
- # $FromClipOrFront: an example program that shows how a hAWK program
- # can be designed to take its input from either the front text window
- # (if you run it through the setup dialog) or from the clipboard
- # (if you run it via a command line).
- # The variable "fromFront" is set to 1 in the "Set variables"
- # dialog for this program, and the command line should read
- # "hAWK -f$FromClipOrFront -vfromFront=0 -n"
- # to signal that input will be from the clip (and no showing of stdout).
-
- # To make this little example more interesting, we send results
- # to stdout if fromFront == 1, and put them back on the clip
- # if fromFront == 0. In order to tell something happened,
- # ProcessInputLine() strips the first character from each input line
- # (useless I know, but visually effective).
-
- BEGIN {
- while (GetLine() > 0) # not getline, GetLine is a function just below
- {
- ProcessInputLine();
- PrintOut();
- }
- FinishUp();
- }
-
- # Get the next line to $0 from file or clipboard.
- function GetLine( retval)
- {
- if (fromFront == 1)
- retval = getline; # use supplied input file(s)
- else
- retval = getClipLine(); # use the clip
- return retval;
- }
-
- # Split up the clip first time, return next line from clip.
- # Return values mimic the behaviour of getline.
- function getClipLine()
- {
- # First time, get the clip and split it into lines.
- if (clip == "")
- {
- clip = getclip();
- numLines = split(clip, lines, "\r");
- # With "split", the last line will be empty if the last
- # character copied was a return.
- if (lines[numLines] == "")
- --numLines;
- if (numLines <= 0)
- return -1;
- }
- ++currentLine;
- if (currentLine > numLines)
- return 0;
- $0 = lines[currentLine];
- return 1;
- }
-
- # Strip first character from input line.
- function ProcessInputLine()
- {
- $0 = substr($0, 2);
- }
-
- # Send output to stdout or variable out.
- function PrintOut()
- {
- if (fromFront == 1)
- print $0;
- else
- out = out $0 "\r"; # or out = out "\r" $0, your choice
- }
-
- # If sending to clip, do so at end.
- function FinishUp()
- {
- if (fromFront == 1)
- ; # we're done
- else # put result back on the clip
- {
- putclip(out);
- }
- }